
#import "SMLoginViewController.h"
#import <Masonry/Masonry.h>
#import <SVProgressHUD/SVProgressHUD.h>
#import "../../Views/Components/SMRoundedButton.h"
#import "../../Managers/Network/SMAPIClient.h"
#import "../../Managers/Auth/SMAuthManager.h"
#import "../Root/SMRootTabBarController.h"

@interface SMLoginViewController ()
@property(nonatomic, strong) UITextField *phoneField;
@property(nonatomic, strong) UITextField *pwdField;
@end

@implementation SMLoginViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"登录/注册";
    self.view.backgroundColor = UIColor.systemBackgroundColor;

    UILabel *title = [UILabel new];
    title.text = @"废钢回收平台";
    title.font = [UIFont boldSystemFontOfSize:24];
    [self.view addSubview:title];

    _phoneField = [UITextField new];
    _phoneField.placeholder = @"手机号";
    _phoneField.keyboardType = UIKeyboardTypePhonePad;
    _phoneField.borderStyle = UITextBorderStyleRoundedRect;

    _pwdField = [UITextField new];
    _pwdField.placeholder = @"密码";
    _pwdField.secureTextEntry = YES;
    _pwdField.borderStyle = UITextBorderStyleRoundedRect;

    SMRoundedButton *loginBtn = [SMRoundedButton new];
    [loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    [loginBtn addTarget:self action:@selector(loginAction) forControlEvents:UIControlEventTouchUpInside];

    UIButton *regBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [regBtn setTitle:@"没有账号？注册" forState:UIControlStateNormal];
    [regBtn addTarget:self action:@selector(registerAction) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:_phoneField];
    [self.view addSubview:_pwdField];
    [self.view addSubview:loginBtn];
    [self.view addSubview:regBtn];
    [self.view addSubview:title];

    [title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop).offset(40);
        make.centerX.equalTo(self.view);
    }];
    [_phoneField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(title.mas_bottom).offset(30);
        make.left.equalTo(self.view).offset(20);
        make.right.equalTo(self.view).offset(-20);
        make.height.mas_equalTo(44);
    }];
    [_pwdField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self->_phoneField.mas_bottom).offset(14);
        make.left.right.height.equalTo(self->_phoneField);
    }];
    [loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self->_pwdField.mas_bottom).offset(20);
        make.left.right.equalTo(self->_pwdField);
        make.height.mas_equalTo(50);
    }];
    [regBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(loginBtn.mas_bottom).offset(10);
        make.centerX.equalTo(self.view);
    }];
}
- (void)loginAction {
    [SVProgressHUD showWithStatus:@"登录中..."];
    NSDictionary *params = @{@"phone": _phoneField.text ?: @"", @"password": _pwdField.text ?: @""};
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [SVProgressHUD dismiss];
        if ([(NSString *)params[@"phone"] length] >= 6) {
            [[SMAuthManager shared] saveToken:@"demo-token-123"];
            UIWindow *win = UIApplication.sharedApplication.keyWindow;
            win.rootViewController = [SMRootTabBarController new];
            [win makeKeyAndVisible];
        } else {
            [SVProgressHUD showErrorWithStatus:@"手机号或密码错误"];
        }
    });
}
- (void)registerAction {
    [SVProgressHUD showWithStatus:@"注册中..."];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [SVProgressHUD showSuccessWithStatus:@"注册成功，请登录"];
    });
}
@end
